package com.ytjj.qmyx.mall.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.filter.IFilterConfig;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.common.utils.SysConfigUtil;
import com.ytjj.qmyx.mall.enums.AppletsPathEnums;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Component
public class AppletsUtil {

    private static String url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=ACCESS_TOKEN";

    @Resource
    private SysConfigUtil sysConfigUtil;

    /**
     * 根据路径跳转 - 小程序(weapp)
     * @param code
     * @return
     */
    public JSONObject skipPath(String code,String channel,String accessToken,Integer pid){
        JSONObject jsonObject = new JSONObject();
        JSONObject jumpWxa = new JSONObject();
        // 根据 code 获取路径
        String path = AppletsPathEnums.getPageName(code);
        // 渠道
        jumpWxa.put("path",path);
        if(AppletsPathEnums.HOT_PRODUCT_DETAIL.getCode().equals(code)){
            if(pid == null){
                throw new ApiException("商品id不存在!");
            }
            jumpWxa.put("query","channel="+channel+"&isPromotion=1"+"&pId="+pid);
        }else{
            jumpWxa.put("query","channel="+channel+"&isPromotion=1");
        }
        jsonObject.put("jump_wxa",jumpWxa);
        jsonObject.put("is_expire",false);
        jsonObject.put("expire_time", DateUtils.addDate(new Date(),1));
        // 发送请求
        String wxUrl = url.replace("ACCESS_TOKEN",accessToken);
        String wxResult = HttpClientUtil.doPostJson(
                wxUrl, JSONObject.toJSONString(jsonObject));
        JSONObject json = JSONObject.parseObject(wxResult);
        log.info("跳转小程序响应：json:{}", JSONObject.toJSONString(json));
        return json;
    }

    /**
     * 根据路径跳转 - 小程序(weapp)
     * @param path
     * @return
     */
    public JSONObject skipByPath(String path,String channel,String accessToken){
        JSONObject jsonObject = new JSONObject();
        JSONObject jumpWxa = new JSONObject();
        // 渠道
        jumpWxa.put("path",path);
        jumpWxa.put("query","channel="+channel+"&isPromotion=1");
        jsonObject.put("jump_wxa",jumpWxa);
        jsonObject.put("is_expire",false);
        jsonObject.put("expire_time", DateUtils.addDate(new Date(),1));
        // 发送请求
        String wxUrl = url.replace("ACCESS_TOKEN",accessToken);
        String wxResult = HttpClientUtil.doPostJson(
                wxUrl, JSONObject.toJSONString(jsonObject));
        JSONObject json = JSONObject.parseObject(wxResult);
        log.info("跳转小程序响应：json:{}", JSONObject.toJSONString(json));
        return json;
    }

    /**
     * 根据路径跳转 - 小程序(weapp)
     * @param path
     * @return
     */
    public JSONObject skipByPath(String path,String channel, String query,String accessToken,String envVersion){
        JSONObject jsonObject = new JSONObject();
        JSONObject jumpWxa = new JSONObject();
        // 渠道
        if (StringUtils.isNotBlank(path)){
            jumpWxa.put("path",path);
        }
        jumpWxa.put("query", query);
        if (StringUtils.isNotBlank(envVersion)){
            jumpWxa.put("env_version",envVersion);
        }
        jsonObject.put("jump_wxa",jumpWxa);
        jsonObject.put("is_expire",false);
        jsonObject.put("expire_time", DateUtils.addDate(new Date(),1));
        // 发送请求
        String wxUrl = url.replace("ACCESS_TOKEN",accessToken);
        String wxResult = HttpClientUtil.doPostJson(
                wxUrl, JSONObject.toJSONString(jsonObject));
        JSONObject json = JSONObject.parseObject(wxResult);
        log.info("跳转小程序响应：json:{}", JSONObject.toJSONString(json));
        return json;
    }

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        JSONObject jumpWxa = new JSONObject();
        // ytjj-sms-100-weapp-discover weixin://dl/business/?t=Z2ZQFxRE4rv
        // ytjj-sms-100-weapp-discover weixin://dl/business/?t=Z2ZQFxRE4rv
        jumpWxa.put("path","/pages/discover/discover");
        jumpWxa.put("query","channel=ytjj-cs&isPromotion=1");
        jsonObject.put("jump_wxa",jumpWxa);
        jsonObject.put("is_expire",true);
        jsonObject.put("expire_time", DateUtils.addDate(new Date(),30));
        log.info("req:{}",JSONObject.toJSONString(jsonObject));
        // 发送请求
        String wxResult = HttpClientUtil.doPostJson(
                "https://api.weixin.qq.com/wxa/generatescheme?access_token=41_-mqT2Pp1FL7rBdOddgpTF8meQNbQWBTGV_qhmEme_5iPnOcfXh8ojPDWF5WLHliiAV_eUtYKNw1x7J2FdYMrCu-WZUIPScZ6z6QWl2gAGxsQ5fsTgOWLSpZ33MQzzOwuKp1BpkLP3lefX1bhNIFaAGAVQN", JSONObject.toJSONString(jsonObject));
        JSONObject json = JSONObject.parseObject(wxResult);
        log.info("登陆响应0：json:{}", JSONObject.toJSONString(json));
    }

}
